package com.sidianzhong.sdz.service.impl;

import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by hxgqh on 2016/1/7.
 */
@Service
@Transactional
public class BannerServiceImpl implements BannerService {
    protected static final Logger LOG = LoggerFactory.getLogger(BannerServiceImpl.class);
    // 将所有的modelMapper注入
    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public Banner create(Banner item) {
        bannerMapper.insert(item);
        return item;
    }

    @Override
    public int delete(Integer id) {
        return bannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Banner item) {
        return bannerMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public Banner get(Integer id) {
        return bannerMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Banner> getListWithPaging(Integer pageNum, Integer pageSize,
                                              String sortItem, String sortOrder, Banner item) {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        if (item.getIntro() != null) {
            criteria.andIntroEqualTo(item.getIntro());
        }
        if (item.getPivotalKey() != null) {
            criteria.andPivotalKeyEqualTo(item.getPivotalKey());
        }
        if (item.getBanner() != null) {
            criteria.andBannerEqualTo(item.getBanner());
        }
        if (item.getStatus() != null) {
            criteria.andStatusEqualTo(item.getStatus());
        }
        if (item.getCreateTime() != null) {
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if (item.getLastUpdateTime() != null) {
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem) + " " + sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<Banner> list = this.bannerMapper.selectByExample(example);
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public List<Banner> getList(Integer pageNum, Integer pageSize,
                                String sortItem, String sortOrder, Banner item) {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        if (item.getIntro() != null) {
            criteria.andIntroEqualTo(item.getIntro());
        }
        if (item.getPivotalKey() != null) {
            criteria.andPivotalKeyEqualTo(item.getPivotalKey());
        }
        if (item.getBanner() != null) {
            criteria.andBannerEqualTo(item.getBanner());
        }
        if (item.getStatus() != null) {
            criteria.andStatusEqualTo(item.getStatus());
        }
        if (item.getCreateTime() != null) {
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if (item.getLastUpdateTime() != null) {
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem) + " " + sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<Banner> list = this.bannerMapper.selectByExample(example);
        return list;
    }

    @Override
    public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder, Banner item) {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        if (item.getIntro() != null) {
            criteria.andIntroEqualTo(item.getIntro());
        }
        if (item.getPivotalKey() != null) {
            criteria.andPivotalKeyEqualTo(item.getPivotalKey());
        }
        if (item.getBanner() != null) {
            criteria.andBannerEqualTo(item.getBanner());
        }
        if (item.getStatus() != null) {
            criteria.andStatusEqualTo(item.getStatus());
        }
        if (item.getCreateTime() != null) {
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if (item.getLastUpdateTime() != null) {
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem) + " " + sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<Banner> list = this.bannerMapper.selectByExample(example);
        List<JSONObject> lists = list.stream().map(banner -> JSONObject.parseObject(JSONObject.toJSONString(banner))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
    }

    @Override
    public String selectValueByKey(String Key) {
        Banner banner = this.bannerMapper.selectByKey(Key);
        if (banner != null) {
            return banner.getBanner();
        }
        return null;
    }

}
